package cn.ffcs.uec.staff.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Tab;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.staff.action.bean.UecStaffMainBean;
import cn.ffcs.uec.staff.constant.UecStaffConstants;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.util.UomZkUtil;

@Controller
@Scope("prototype")
public class UecStaffMainComposer extends BasePortletComposer implements
		IPortletInfoProvider {

	private static final long serialVersionUID = -5929030282722775845L;

	private UecStaffMainBean bean = new UecStaffMainBean();

	private UecStaffInfo uecStaffInfo;
	
	/**
	 * 操作类型.
	 */
	private String opType = null; // 操作类型

	/**
	 * 关闭窗口标志
	 */
	private Boolean onCloseFlag;
	
	@Override
	public String getPortletId() {
		return super.getPortletId();
	}

	@Override
	public ThemeDisplay getThemeDisplay() {
		return super.getThemeDisplay();
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		UomZkUtil.autoFitHeight(comp);// 使用<borderlayout>时，需要加上这句进行初始化，不然从仓库调出的页面，不能显示
		Components.wireVariables(comp, bean);
		bean.getUecStaffInfoListboxExt().setPortletInfoProvider(this);
		bean.getUecStaffInfoListboxExt().addForward(
				UecStaffConstants.ON_UEC_STAFF_INFO_QUERY, comp,
				UecStaffConstants.ON_UEC_STAFF_INFO_QUERY_RESPONSE);
		
		bean.getUecStaffInfoListboxExt().addForward("onCloseStaffMainWindown", comp,
				"onCloseStaffMainWindownResponse");

		bean.getUecStaffInfoListboxExt().addForward(
				UecStaffConstants.ON_CLEAN_TAB_UEC_STAFF_INFO_QUERY, comp,
				UecStaffConstants.ON_CLEAN_TAB_UEC_STAFF_INFO_QUERY_RESPONSE);
	}

	/**
	 * 界面初始化
	 * @throws Exception 
	 */
	public void onCreate$uecStaffMainWin() throws Exception {
		bindBean();
	}
	
	/**
	 * 监听关闭窗口事件
	 * @param event
	 */
	public void onCloseStaffMainWindownResponse(final ForwardEvent event) {
		onCloseFlag = (Boolean) event.getOrigin().getData();
		if(onCloseFlag) {
			bean.getUecStaffMainWin().onClose();
		}
	}
	
	/**
	 * 
	 * @throws Exception
	 */
	public void bindBean() throws Exception {
		opType = StrUtil.strnull(arg.get("opType"));
		uecStaffInfo = (UecStaffInfo) arg.get("uecStaffInfo");
		if ("view".equals(opType)) {
			if (uecStaffInfo != null) {
				Events.postEvent("onSelectUecStaffInfoByGetOrgTree", this.bean.getUecStaffInfoListboxExt(), uecStaffInfo);
			}
		}
	}

	/**
	 * 查询到员工基本信息的响应处理.
	 * 
	 * @param event
	 */
	public void onUecStaffInfoQueryResponse(final ForwardEvent event) {
		uecStaffInfo = (UecStaffInfo) event.getOrigin().getData();
		if (uecStaffInfo != null) {
			callTab();
		}
	}

	public void onClickTab(ForwardEvent event) {
		try {
			Event origin = event.getOrigin();
			if (origin != null) {
				Component comp = origin.getTarget();
				if (comp != null && comp instanceof Tab) {
					bean.setTab((Tab) comp);
					callTab();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * tab响应
	 * 
	 * @throws Exception
	 * 
	 */
	public void callTab() {
		try {
			if (bean.getTab() == null) {
				bean.setTab(bean.getTabBox().getSelectedTab());
			}
			if (uecStaffInfo != null) {
				String tab = this.bean.getTab().getId();
				if ("uecEncTab".equals(tab)) {
					Events.postEvent(UecStaffConstants.ON_UEC_ENC_QUERY,
							this.bean.getUecEncListboxExt(), uecStaffInfo);
				} else if ("uecUecTalentTeamTab".equals(tab)) {
					Events.postEvent(
							UecStaffConstants.ON_UEC_TALENT_TEAM_QUERY,
							this.bean.getUecTalentTeamListboxExt(),
							uecStaffInfo);
				} else if ("uecAssTab".equals(tab)) {
					Events.postEvent(UecStaffConstants.ON_UEC_ASS_QUERY,
							this.bean.getUecAssListboxExt(), uecStaffInfo);
				} else if ("uecDocEduTab".equals(tab)) {
					Events.postEvent(UecStaffConstants.ON_UEC_DOC_EDU_QUERY,
							this.bean.getUecDocEduListboxExt(), uecStaffInfo);
				} else if ("uecPartyLogTab".equals(tab)) {
					Events.postEvent(UecStaffConstants.ON_UEC_PARTYLOG_QUERY,
							this.bean.getUecPartyLogListboxExt(), uecStaffInfo);
				} else if ("uecPunishTab".equals(tab)) {
					Events.postEvent(UecStaffConstants.ON_UEC_PUNISH_QUERY,
							this.bean.getUecPunishListboxExt(), uecStaffInfo);
				} else if ("uecWorkTab".equals(tab)) {
					Events.postEvent(UecStaffConstants.ON_UEC_WORK_QUERY,
							this.bean.getUecWorkListboxExt(), uecStaffInfo);
				} else if ("uecPsnjobTab".equals(tab)) {
					Events.postEvent(UecStaffConstants.ON_UEC_PSNJOB_QUERY,
							this.bean.getUecPsnjobListboxExt(), uecStaffInfo);
				} else if ("uecRetireTab".equals(tab)) {
					Events.postEvent(UecStaffConstants.ON_UEC_RETIRE_QUERY,
							this.bean.getUecRetireListboxExt(), uecStaffInfo);
				} else if ("uecPsnorgTab".equals(tab)) {
					Events.postEvent(UecStaffConstants.ON_UEC_PSNORG_QUERY,
							this.bean.getUecPsnorgListboxExt(), uecStaffInfo);
				} else if ("uecPsndocWadocTab".equals(tab)) {
					Events.postEvent(
							UecStaffConstants.ON_UEC_PSNDOC_WADOC_QUERY,
							this.bean.getUecPsndocWadocListboxExt(),
							uecStaffInfo);
				} else if ("uecPaymentDataTab".equals(tab)) {
					Events.postEvent(
							UecStaffConstants.ON_UEC_PAYMENT_DATA_QUERY,
							this.bean.getUecPaymentDataListboxExt(),
							uecStaffInfo);
				} else if ("uecBmDataTab".equals(tab)) {
					Events.postEvent(UecStaffConstants.ON_UEC_BM_DATA_QUERY,
							this.bean.getUecBmDataListboxExt(), uecStaffInfo);
				}
				// 2018-01-08
				else if ("uecAbroadTab".equals(tab)) {
					Events.postEvent(UecStaffConstants.ON_UEC_ABROAD_QUERY,
							this.bean.getUecAbroadListboxExt(), uecStaffInfo);
				} else if ("uecCtrtTab".equals(tab)) {
					Events.postEvent(UecStaffConstants.ON_UEC_CTRT_QUERY,
							this.bean.getUecCtrtListboxExt(), uecStaffInfo);
				} else if ("uecFamilyTab".equals(tab)) {
					Events.postEvent(UecStaffConstants.ON_UEC_FAMILY_QUERY,
							this.bean.getUecFamilyListboxExt(), uecStaffInfo);
				} else if ("uecRetriedTab".equals(tab)) {
					Events.postEvent(UecStaffConstants.ON_UEC_RETRIED_QUERY,
							this.bean.getUecRetriedListboxExt(), uecStaffInfo);
				} else if ("uecHightTeamTab".equals(tab)) {
					Events.postEvent(UecStaffConstants.ON_UEC_HiGHTTEAM_QUERY,
							this.bean.getUecHightTeamListboxExt(), uecStaffInfo);
				} else if ("uecNationdutyTab".equals(tab)) {
					Events.postEvent(
							UecStaffConstants.ON_UEC_NATION_DUTY_QUERY,
							this.bean.getUecNationdutyListboxExt(),
							uecStaffInfo);
				} else if ("uecTitleTab".equals(tab)) {
					Events.postEvent(UecStaffConstants.ON_UEC_TITLE_QUERY,
							this.bean.getUecTitleListboxExt(), uecStaffInfo);
				} else if ("uecTrainTab".equals(tab)) {
					Events.postEvent(UecStaffConstants.ON_UEC_TRAIN_QUERY,
							this.bean.getUecTrainListboxExt(), uecStaffInfo);
				} else if ("uecTrialTab".equals(tab)) {
					Events.postEvent(UecStaffConstants.ON_UEC_TRIAL_QUERY,
							this.bean.getUecTrialListboxExt(), uecStaffInfo);
				}
			} else {
				// 清空数据
				bean.getUecEncListboxExt().onCleanUecEncResponse(null);
				bean.getUecTalentTeamListboxExt().onCleanUecTalentTeamResponse(
						null);
				bean.getUecAssListboxExt().onCleanUecAssResponse(null);
				bean.getUecDocEduListboxExt().onCleanUecDocEduResponse(null);
				bean.getUecPartyLogListboxExt()
						.onCleanUecPartyLogResponse(null);
				bean.getUecPunishListboxExt().onCleanUecPunishResponse(null);
				bean.getUecWorkListboxExt().onCleanUecWorkResponse(null);
				bean.getUecPsnjobListboxExt().onCleanUecPsnjobResponse(null);
				bean.getUecRetireListboxExt().onCleanUecRetireResponse(null);
				bean.getUecPsnorgListboxExt().onCleanUecPsnorgResponse(null);
				bean.getUecPsndocWadocListboxExt()
						.onCleanUecPsndocWadocResponse(null);
				bean.getUecPaymentDataListboxExt()
						.onCleanUecPaymentDataResponse(null);
				bean.getUecBmDataListboxExt().onCleanUecBmDataResponse(null);
				// 2018-01-08
				bean.getUecAbroadListboxExt().onCleanUecAbroadResponse(null);
				bean.getUecCtrtListboxExt().onCleanUecCtrtResponse(null);
				bean.getUecFamilyListboxExt().onCleanUecFamilyResponse(null);
				bean.getUecRetriedListboxExt().onCleanUecRetriedResponse(null);
				bean.getUecHightTeamListboxExt().onCleanUecHightTeamResponse(
						null);
				bean.getUecNationdutyListboxExt().onCleanUecNationdutyResponse(
						null);
				bean.getUecTitleListboxExt().onCleanUecTitleResponse(null);
				bean.getUecTrainListboxExt().onCleanUecTrainResponse(null);
				bean.getUecTrialListboxExt().onCleanUecTrialResponse(null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 处理点击后Tabs .
	 * 
	 * @param event
	 */
	public void onCleanTabUecStaffInfoQueryResponse(final ForwardEvent event) {
		uecStaffInfo = (UecStaffInfo) event.getOrigin().getData();
		if (uecStaffInfo == null) {
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_ENC,
					bean.getUecEncListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_ASS,
					bean.getUecAssListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_DOC_EDU,
					bean.getUecDocEduListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_TALENT_TEAM,
					bean.getUecDocEduListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_PARTYLOG,
					bean.getUecPartyLogListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_PUNISH,
					bean.getUecPunishListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_WORK,
					bean.getUecWorkListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_PSNJOB,
					bean.getUecPsnjobListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_RETIRE,
					bean.getUecRetireListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_PSNORG,
					bean.getUecPsnorgListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_PSNDOC_WADOC,
					bean.getUecPsndocWadocListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_PAYMENT_DATA,
					bean.getUecPaymentDataListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_BM_DATA,
					bean.getUecBmDataListboxExt(), null);
			// 2018-01-08
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_ABROAD,
					bean.getUecAbroadListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_CTRT,
					bean.getUecCtrtListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_FAMILY,
					bean.getUecFamilyListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_RETRIED,
					bean.getUecRetriedListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_HiGHTTEAM,
					bean.getUecHightTeamListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_NATION_DUTY,
					bean.getUecNationdutyListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_TITLE,
					bean.getUecTitleListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_TRAIN,
					bean.getUecTrainListboxExt(), null);
			Events.postEvent(UecStaffConstants.ON_CLEAN_UEC_TRIAL,
					bean.getUecTrialListboxExt(), null);

			callTab();
		}
	}

}
